home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / app_specific / MyImageWindowController.m < prev    next >
Encoding:
Text File  |  2002-05-22  |  6.7 KB  |  173 lines

  1. /*
  2. macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: MyImageWindowController.m,v 1.1.1.1 2002/05/22 04:56:42 dirkx Exp $
  19.  */
  20.  
  21.  
  22.  
  23. #import "MyImageWindowController.h"
  24. #import "MyImageDocument.h"
  25. #import "MyScrollView.h"
  26. #include "GlobalDefs.h"
  27.  
  28. static NSString*     ImageToolbarIdentifier        = @"Image Window Toolbar Identifier";
  29. static NSString*     RotateCWToolbarItemIdentifier    = @"Image Clockwise Rotation Item Identifier";
  30. static NSString*     RotateCCWToolbarItemIdentifier    = @"Image Counter-Clockwise Rotation Item Identifier";
  31.  
  32. @implementation MyImageWindowController
  33.  
  34. - (void) windowDidLoad {
  35.     [super windowDidLoad];
  36.     [[self window] setDelegate:self];                //We set the standard size
  37.     [self setupToolbar];
  38.     [[NSNotificationCenter defaultCenter] addObserver:self
  39.                                              selector:@selector(documentChanged:)
  40.                                                  name:@"Document changed notification"
  41.                                                object:[self document]];
  42. }
  43.  
  44. - (void) documentChanged:(NSNotification*)notification {
  45.     MyScrollView* scrollView=[[[[self window] contentView] subviews] objectAtIndex:0];
  46.     [scrollView setImageRep:[(MyImageDocument*)[self document] imageRep]];
  47.     [self resizeWindowToContent];
  48. }
  49.  
  50. - (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)newFrame {
  51.     NSRect innerFrame,winFrame;
  52.     NSSize additionalSize;
  53.     MyScrollView* scrollView=[[[window contentView] subviews] objectAtIndex:0];
  54.     winFrame=[window frame];
  55.     innerFrame=[[window contentView] frame];
  56.  
  57. /*Determine the additional size of window title, border etc. The standard way to do this - [NSWindow frameRectForContentRect] doesn't take toolbars into account... Does someeone have a clue how to find out the current vertical size of a toolbar? */
  58.     
  59.     additionalSize.width=winFrame.size.width-innerFrame.size.width;
  60.     additionalSize.height=winFrame.size.height-innerFrame.size.height;
  61.  
  62. //Determine the best new size 
  63.     innerFrame.size=[[[scrollView documentView] image] size];
  64.     innerFrame.size.width+=additionalSize.width;
  65.     innerFrame.size.height+=additionalSize.height;
  66.  
  67. //Should expand to bottom
  68.     winFrame.origin.y+=winFrame.size.height-innerFrame.size.height;
  69.  
  70. //Apply new size to old origin
  71.     winFrame.size=innerFrame.size;
  72.     return winFrame;
  73. }
  74.  
  75. - (void) resizeWindowToContent {
  76.     NSRect rect=NSMakeRect(0,0,0,0);
  77.     NSRect screenFrame=[[[self window] screen] frame];
  78.     rect=[self windowWillUseStandardFrame:[self window] defaultFrame:rect];
  79. //The resulting window shouldn't be wider than the screen
  80.     if (rect.size.width>screenFrame.size.width) {
  81.         rect.size.width=screenFrame.size.width;
  82.     }
  83. //The resulting window shouldn't be left out-of-the-screen
  84.     if (rect.origin.x<screenFrame.origin.x) {
  85.         rect.origin.x=screenFrame.origin.x;
  86.     }
  87. //The resulting window shouldn't be right out-of-the-screen
  88.     if (rect.origin.x+rect.size.width>screenFrame.origin.x+screenFrame.size.width) {
  89.         rect.origin.x=screenFrame.origin.x+screenFrame.size.width-rect.size.width;
  90.     }
  91. //Vertical matching is done by [window constrainFrameRect...]
  92.     [[self window] setFrame:rect display:YES];
  93. }
  94.  
  95. - (void) magnify50:(id)sender {
  96.     MyScrollView* scrollView=[[[[self window] contentView] subviews] objectAtIndex:0];
  97.     [scrollView setZoomFactor:0.5f];
  98. }
  99.  
  100. - (void) magnify100:(id)sender {
  101.     MyScrollView* scrollView=[[[[self window] contentView] subviews] objectAtIndex:0];
  102.     [scrollView setZoomFactor:1.0f];
  103. }
  104.  
  105. - (void) magnify200:(id)sender {
  106.     MyScrollView* scrollView=[[[[self window] contentView] subviews] objectAtIndex:0];
  107.     [scrollView setZoomFactor:2.0f];
  108. }
  109.  
  110. - (void) magnifyLarger:(id)sender {
  111.     MyScrollView* scrollView=[[[[self window] contentView] subviews] objectAtIndex:0];
  112.     [scrollView setZoomFactor:[scrollView zoomFactor]*2.0f];
  113. }
  114.  
  115. - (void) magnifySmaller:(id)sender {
  116.     MyScrollView* scrollView=[[[[self window] contentView] subviews] objectAtIndex:0];
  117.     [scrollView setZoomFactor:[scrollView zoomFactor]/2.0f];
  118. }
  119.  
  120. - (void) setupToolbar {
  121.     NSToolbar *toolbar = [[[NSToolbar alloc] initWithIdentifier: ImageToolbarIdentifier] autorelease];
  122.     [toolbar setAllowsUserCustomization: YES];
  123.     [toolbar setAutosavesConfiguration: YES];
  124.     [toolbar setDisplayMode: NSToolbarDisplayModeIconOnly];
  125.     [toolbar setDelegate: self];
  126.     [[self window] setToolbar: toolbar];
  127. }
  128.  
  129. - (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
  130.  
  131.     NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
  132.  
  133.     if ([itemIdent isEqual:RotateCWToolbarItemIdentifier]) {
  134.         [toolbarItem setLabel: LStr(@"Rotate right")];
  135.         [toolbarItem setPaletteLabel: LStr(@"Rotate right")];
  136.         [toolbarItem setToolTip: LStr(@"Turn 90 degrees clockwise")];
  137.         [toolbarItem setImage: [NSImage imageNamed: @"RotateCWToolbarItem"]];
  138.         [toolbarItem setTarget: [self document]];
  139.         [toolbarItem setAction: @selector(rotateCW:)];
  140.     } else if ([itemIdent isEqual:RotateCCWToolbarItemIdentifier]) {
  141.         [toolbarItem setLabel: LStr(@"Rotate left")];
  142.         [toolbarItem setPaletteLabel: LStr(@"Rotate left")];
  143.         [toolbarItem setToolTip: LStr(@"Turn 90 degrees counter-clockwise")];
  144.         [toolbarItem setImage: [NSImage imageNamed: @"RotateCCWToolbarItem"]];
  145.         [toolbarItem setTarget: [self document]];
  146.         [toolbarItem setAction: @selector(rotateCCW:)];
  147.     } else {
  148.         toolbarItem = NULL;
  149.     }
  150.     return toolbarItem;
  151. }
  152.  
  153. - (NSArray *) toolbarDefaultItemIdentifiers: (NSToolbar *) toolbar {
  154.     return [NSArray arrayWithObjects:
  155.         RotateCWToolbarItemIdentifier,
  156.         RotateCCWToolbarItemIdentifier,
  157.         NULL];
  158. }
  159.  
  160. - (NSArray *) toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar {
  161.     return [NSArray arrayWithObjects:
  162.         RotateCWToolbarItemIdentifier,
  163.         RotateCCWToolbarItemIdentifier,
  164.         NULL];
  165. }
  166.  
  167. - (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem {
  168.     return YES;
  169. }
  170.  
  171.  
  172. @end
  173.